package reports;

import common.Result;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.ArrayList;

import model.entity.Item;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.pdf.BarcodeEAN;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

/**
 * @author Trevor Dixon
 * 
 *         A sheet of barcodes.
 * 
 */
public class BarcodeSheet
{
  private List<Item> items = new ArrayList<Item>();

  public BarcodeSheet()
  {

  }

  /**
   * @param itemIterator
   *          An <code>Iterator</code> of barcodes for which labels will be generated and added to
   *          the sheet.
   */
  public BarcodeSheet(java.util.Iterator<Item> itemIterator)
  {
    while (itemIterator.hasNext())
    {
      addItem(itemIterator.next());
    }
  }

  /**
   * @param item
   *          The barcode to be added.
   * @return A <code>Result</code> indicating whether the operation was successful.
   */
  public Result addItem(Item item)
  {
    try
    {
      items.add(item);
    }
    catch (Exception e)
    {
      return new Result(false, e.getMessage());
    }
    return new Result(true);
  }

  /**
   * @param path
   *          The path to which the PDF will be saved.
   * @return A <code>Result</code> indicating whether the PDF was successfully created and saved.
   * @throws DocumentException
   * @throws FileNotFoundException
   */
  public Result createPDF(java.nio.file.Path path) throws DocumentException, FileNotFoundException
  {
    SimpleDateFormat dateFormatter = new SimpleDateFormat("mm/dd/yy");

    Document document = new Document();
    document.setMargins(0, 0, 30, 30);

    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(path.toFile()));
    document.open();

    PdfPTable table = new PdfPTable(6);
    PdfContentByte cb = writer.getDirectContent();

    BarcodeEAN codeEAN = new BarcodeEAN();
    codeEAN.setCodeType(com.itextpdf.text.pdf.Barcode.UPCA);
    codeEAN.setCode("4512345678906");

    table.setTotalWidth(90 * 6);
    table.setLockedWidth(true);

    Font font = new Font(FontFamily.HELVETICA, 7);

    for (Item item : items)
    {
      codeEAN.setCodeType(com.itextpdf.text.pdf.Barcode.UPCA);
      codeEAN.setCode(item.getBarcode().getCode());

      String description = item.getProduct().getDescription();

      if (description.length() > 19)
      {
        description = description.substring(0, 19);
      }

      Phrase p1 = new Phrase(description, font);

      // String entryAndExpDate = dateFormatter.format(item.getEntryDate());
      //
      // if (item.getExpirationDate() != null)
      // {
      // entryAndExpDate += " exp " + dateFormatter.format(item.getExpirationDate());
      // }

      Phrase p2 = new Phrase(6, dateFormatter.format(item.getEntryDate()), font);

      Image image = codeEAN.createImageWithBarcode(cb, null, null);

      PdfPCell cell = new PdfPCell();
      // cell.setNoWrap(true);

      cell.addElement(p1);
      cell.addElement(p2);
      cell.addElement(image);
      cell.setPaddingRight(6);
      cell.setPaddingBottom(6);
      cell.setBorder(0);
      table.addCell(cell);
    }

    int restofrow = 6 - items.size() % 6;

    for (int j = 0; j < restofrow; j++)
    {
      PdfPCell cell2 = new PdfPCell();
      cell2.setBorder(0);
      table.addCell(cell2);
    }

    document.add(table);
    document.close();

    return new Result(false, "Not implemented.");
  }
}
